package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import tk.mybatis.mapper.entity.Example;
@Service
public class IVerifyImpl implements IVerifyService {
  @Autowired
  private JavaMailSender javaMailSender;

    /**
     * 给用户发送邮件
     * 1 现在还需需要取出用户注册的邮件 写在 userEmail 处
     * 2 需要把之前插入导用户注册表的 uuid 获得到, 并写入 uuid 处
     * 3 获得username 插入
     */
    @Override
    public void sentEmail(String username,String uid,String userEmail) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("959649729@qq.com");
        simpleMailMessage.setTo(userEmail);
        simpleMailMessage.setSubject("商场激活");
        simpleMailMessage.setText("http://localhost:8080/user/verify?uid="+uid+"&username="+username);
        javaMailSender.send(simpleMailMessage);
    }

    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;
    /**
     * 用户点击邮件链接后的 认证
     * @param uid  链接自带的uid,不会为空
     * @param username 链接自带
     *
     */
    @Override
    public void verify(String uid,String username) {


        Example example = new Example(UserVerify.class);
        example.createCriteria()
                .andEqualTo("uid",uid)
                .andEqualTo("isExpire","N");

        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");


        //修改member表
        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username",username);

        Member member = new Member();
        member.setIsVerified("Y");
        memberMapper.updateByExampleSelective(member,memberExample);

        userVerifyMapper.updateByExampleSelective(userVerify, example);

    }
}
